<template>
	<om-page>
		<uni-list>
			<uni-list-item @tap="goResetPwd">
				{{'修改密码'}}
			</uni-list-item>
			<uni-list-item @tap="goChangeUrl">
				<view>登录网址</view>
			</uni-list-item>
			<uni-list-item @tap="goPrivacy">
				<view>隐私政策</view>
			</uni-list-item>
			<uni-list-item @tap="goAgreement">
				<view>用户协议</view>
			</uni-list-item>
			<uni-list-item @tap="accountCancellation">
				<view>账号注销</view>
			</uni-list-item>
		</uni-list>
		<view  class="padding flex om-bottom-btn" style="justify-content: space-between;z-index:999">
			<button class="om-bg"  style="color: #FFFFFF;width:50% " v-if="hasLogin" @tap="bindLogout">退出登录</button>
			<button class="om-cancel margin-left" style="border: 1px solid #5697F4;width:50% " v-if="hasLogin" @tap="goBack">返回</button>
		</view>
	</om-page>
</template>

<script>
	import { mapState, mapActions } from 'pinia';
	import {
			useLoginUserStore
		} from '@/stores/login-user.js';
	export default{
		computed:{
			...mapState(useLoginUserStore,['hasLogin', 'forcedLogin', 'userName','userId',"avatar","sexCode"
				// #ifdef MP-WEIXIN
				,'db','openid','orgCode'
				// #endif
			])
		},
		methods:{
			...mapActions(useLoginUserStore, ['logout'
			// #ifdef MP-WEIXIN
			,'setOrgCode','setSubUr','setUserId'
			// #endif
			]),
			clearPushClientId(){
				// #ifdef APP-PLUS || APP || APP-VUE
				let _that = this;
				_that.om.request('temployee/clearPushClientId',{},'post').then(res => {
					if (res.succeed) {
						
					}
				});
				// #endif
			},
			accountCancellation(){
				let _that = this;
				uni.showModal({
					title: '账号注销',
					content: '是否确定进行账号注销操作，注销后将无法登录！',
					showCancel: true,
					success(res){
						if(res.confirm){
							_that.om.request('temployeeLogOffShow/addLogoff',{
								logoffUserId: _that.userId
							},'post').then(res => {
								if (res.succeed) {
									_that.om.request('loginCenter/h5Logout').then(res => {
										if (res.succeed) {
											_that.logout();
											_that.om.clearToken();
											uni.removeStorageSync("ORG_SITE");
											_that.clearPushClientId()
											if (_that.forcedLogin) {
												uni.reLaunch({
													url: '../login/login/login'
												});
											}
										}
									});
								}else{
									_that.om.toast(res.msg)
								}
							});
						}
					}
				})
			},
			goPrivacy(){
				uni.navigateTo({
					url: '/pages/login/privacypolicy/privacy-policy'
				})
			},
			goAgreement(){
				uni.navigateTo({
					url: '/pages/login/privacypolicy/user-agreement'
				})
			},
			goBack(){
				uni.navigateBack({
					
				})
			},
			goResetPwd(){
				uni.navigateTo({
					url:'../setting/resetPwd'
				})
			},
			goChangeUrl(){
				uni.navigateTo({
					url:'../setting/changeUrl'
				})
			},
			// #ifdef H5 || APP-PLUS || APP || APP-VUE
			bindLogout() {
				let _that = this;
				_that.om.request('loginCenter/h5Logout').then(res => {
					if (res.succeed) {
						_that.logout();
						_that.om.clearToken();
						uni.removeStorageSync("ORG_SITE");
						_that.clearPushClientId()
						if (_that.forcedLogin) {
							uni.reLaunch({
								url: '../login/login/login'
							});
						}
					}
				});
			}
			// #endif
			// #ifdef MP-WEIXIN
			,bindLogout(){
				let _that = this
				_that.om.request('wxlogin/unbind', {
					reason: "退出登录"
				},'post').then(d=>{
					if(d.succeed) {
						  try {
						    _that.db.collection('accounts').where({
						      "_openid": _that.openid
						    }).get().then(res => {
						      if (res.data.length) {
						        var d = res.data[0]
						        if(d.orges.length > 1){
						          var orges = []
						          for (var i = 0; i < d.orges.length;i++){
						            var org = d.orges[i]
						            if(org.orgCode !== _that.orgCode){
						              orges.push(org)
						            }
						          }
						          const _ = _that.db.command
						          _that.db.collection('accounts').doc(d._id).update({
						            // data 传入需要局部更新的数据
						            data: {
						              orges: _.set(orges)
						            },
						            success() {
										_that.clearPushClientId()
										_that.setOrgCode("")
										_that.setSubUr("")
										_that.setUserId("")
										  uni.reLaunch({
										  	url: '../login/login/login'
										  });
						            },
						            fail: function (fail) {
						              console.log(fail)
						            }
						          })
						        }else{
								  _that.clearPushClientId()
						          _that.db.collection('accounts').doc(res.data[0]._id).remove()
						          _that.setOrgCode("")
						          _that.setSubUr("")
						          _that.setUserId("")
						            uni.reLaunch({
						            	url: '../login/login/login'
						            });
						        }
						      }
						    })
						  } catch (e) {
						    console.error(e)
						  }
					}
				})
			}
			// #endif
		}
	}
</script>

<style>
</style>                                                  
